package com.example.test17excel.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: fxy
 * @Date: 2022/04/06/16:28
 * @Description:
 */
public class DataListener extends AnalysisEventListener {
    final static int BATCH_COUNT = 100;
    List list = new ArrayList<>();
    @Override
    public void invoke(Object o, AnalysisContext analysisContext) {
        System.out.println(Thread.currentThread().getName());
        list.add(o);
        System.out.println(o);
        // 达到BATCH_COUNT了，需要去存储一次数据库，防止数据几万条数据在内存，容易OOM
        if (list.size() >= BATCH_COUNT) {

            //saveData();
            // 存储完成清理 list
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        //
    }
}
